<template>
  <div>
    <el-input v-model="searchText" placeholder="请输入每日一句内容或作者" width="150"></el-input>
    <el-button @click="fetchList()">搜索</el-button>
    <el-button class="mb-3" type="primary" @click="addTagDialogVisible = true">添加每日一句</el-button>
    <el-dialog :visible.sync="addTagDialogVisible" title="添加每日一句">
      <el-form :model="addTagForm" label-width="80px">
        <el-form-item label="content">
          <el-input v-model="addTagForm.content"></el-input>
        </el-form-item>
        <el-form-item label="author">
          <el-input v-model="addTagForm.author"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addTagDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addTip">添加</el-button>
      </div>
    </el-dialog>
    <el-table :data="tags" style="width: 100%">
      <el-table-column label="id" prop="id"></el-table-column>
      <el-table-column label="content" prop="content"></el-table-column>
      <el-table-column label="author" prop="author"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button round type="warning" @click="editTag(scope.row)">编辑</el-button>
          <el-button round type="danger" @click="deleteTip(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="editTagDialogVisible" title="编辑每日一句">
      <el-form :model="editTagForm" label-width="80px">
        <el-form-item label="content">
          <el-input v-model="editTagForm.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editTagDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateTip">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getTip, addTip, updateTip, deleteTip} from '@/api/tip'

export default {
  name: 'TipList',
  data() {
    return {
      tags: [],
      searchText: '',
      addTagDialogVisible: false,
      addTagForm: {
        content: '',
        author: '',
      },
      editTagDialogVisible: false,
      editTagForm: {
        id: '',
        content: '',
        author: '',
      }
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      getTip(decodeURIComponent(this.searchText)).then(response => {
        this.tags = response.data
      })
    },
    addTip() {
      addTip(this.addTagForm).then(() => {
        this.addTagDialogVisible = false
        this.fetchList()
      })
    },
    editTag(row) {
      this.editTagForm.id = row.id
      this.editTagForm.content = row.content
      this.editTagForm.author = row.author
      this.editTagDialogVisible = true
    },
    updateTip() {
      updateTip(this.editTagForm).then(() => {
        this.editTagDialogVisible = false
        this.fetchList()
      })
    },
    deleteTip(row) {
      this.$confirm(`确认删除 ${row.content} 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTip(row.id).then(() => {
          this.fetchList()
        })
      })
    }
  }
}
</script>